var table;
$(function(){
    table=$("#roleGroup").DataTable({
        "ajax": {
            url:"../../admin/role/Get",
            type:"POST",
            data:function(d){
                d.roleName=$("#search_value").val()
            },error:function(XMLHttpRequest,textStatus,errorThrown){
                console.log("载入数据出错");
            }
        },
        "scrollX": 1000,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": true,
        "bAutoWidth": true,
        "bInfo": true,//页脚信息
        "bFilter": false,//禁用搜索
        "bSort":false,//禁用排序
        "aLengthMenu": [2,3,5],
        "sPaginationType":'full_numbers',
        "columns": [
            {"data":"id"},
            { "data": "roleName" },
            { "data": "roleDescribe" },
            { "data": "postTime" },
            { "data": "id" },
        ],
        "aoColumnDefs": [ {
            "aTargets": [ 2 ],
            "mRender": function ( data, type, full ) {
                return data
            }
        },{
            "aTargets": [ 3 ],
            "mRender": function ( data, type, full ) {
                return data
            }
        },
            {
                "aTargets": [ 4 ],
                "mRender": function ( data, type, full ) {
                    return '<a onclick="editGroupData(\''+full.id+'\',\''+full.roleName+'\',\''+full.roleDescribe+'\')">编辑</a>' +
                        '<a style="margin-left: 12px" onclick="delGroupData(\''+full.id+'\')">删除</a>';
                }
            } ],
        "fnRowCallback": function (nRow, aData, iDisplayIndex) {
            $("td:first", nRow).html(iDisplayIndex + 1);
            return nRow;
        },
        "oLanguage": {
            "sProcessing": "处理中...",
            "sLengthMenu": "每页 _MENU_ 条",
            "sZeroRecords": "没有匹配结果",
            "sInfo": " _START_ 至 _END_ 项结果，共 _TOTAL_ 项",//显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项
            "sInfoEmpty": "",//显示第 0 至 0 项结果，共 0 项
            "sInfoFiltered": "",//(由 _MAX_ 项结果过滤)
            "sInfoPostFix": "",
            "sSearch": "搜索：",
            "sUrl": "",
            "sEmptyTable": "没有数据呀!",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            }
        },
    });
})
new SMValidator('#addGroupFrom');

function searchGroupBtn(){
    table.draw();
}

function addGroupBtn() {
    $(".modal-title").text("新增用户组");
    $("#id").val("");
    $("#add_Group").val("");
    $("#add_GroupDes").val("");
    $("input[name='ch_box']").prop("checked",false);
    $("#GroupModal").modal("show");
}
function groupmodal_sub(){
    //组json对象只有一个对象【0】
    var eachData = {};
    eachData["_csrf"]=$("input[name=_csrf]").val();
    eachData["id"] = $("#id").val();
    eachData["roleName"] = $("#add_Group").val();
    eachData["roleDescribe"] = $("#add_GroupDes").val();
    $("input[name='ch_box']").each(
        function(){
            if ($(this).prop("checked") == true){
                eachData["Res"+$(this).attr("value")] = true;
            }
        }
    )
    //json对象转查询字符串
    var sendStr = "";
    for(var key in eachData){
        if (key=="_csrf"){
            sendStr += key + '=' + eachData[key];
        }else{
            sendStr += '&' + key + '=' + eachData[key];
        }
    }

    if(SMValidator.validate('#addGroupFrom')){
        $.ajax({
            url: "../../admin/role",
            type: "POST",
            dataType: "json",
            data: sendStr,
            beforeSend: function () {
            },
            success: function (data) {
                if(data){
                    var msg = "新增成功"

                    Lobibox.notify('success', {
                        msg: msg,
                        sound: false
                    });
                    $("#GroupModal").modal("hide")
                    table.draw();
                }else {
                    console.log(data)
                }
            }, error: function () {
            }
        });
    }
}

function editGroupData(id,val,des) {
    $(".modal-title").text("修改属性");
    $("#id").val(id);
    $("#add_Group").val(val);
    $("#add_GroupDes").val(des);
    searchGroupResBtn();
    $("#GroupModal").modal("show");
}

function delGroupData(id){
    Lobibox.confirm({
        msg: "确认要删除此用户组?",
        title:"删除用户组",
        iconClass: false,
        callback: function ($this, type, ev) {
            if(type==='yes'){
                $.ajax({
                    url: "../../admin/role/"+id+"?_csrf="+$("input[name=_csrf]").val(),
                    type: "DELETE",
                    dataType: "json",
                    success: function (data) {
                        if(data){
                            table.draw();
                            Lobibox.notify('success', {
                                msg: '删除成功',
                                sound: false
                            });
                        }
                        else if (data == -1) {
                            Lobibox.notify('error', {
                                msg: '请先移除此用户组下用户',
                                sound: false
                            });
                        }
                        else {
                            console.log(id);
                            Lobibox.notify('error', {
                                msg: '删除失败',
                                sound: false
                            });
                        }
                       
                    }, 
                    error: function () {

                    }
                });
            }
        }
    });
}
//modal2
function editResBtn() {
    $("#GroupModal").modal("hide");
    $("#ResModal").modal("show");
}
function searchGroupResBtn() {
    var value = $("#add_Group").val();
    value = $.trim(value);
    if ($("#add_Group").val()==null||$("#add_Group").val()==''||value=='') {
        Lobibox.notify('error', {
            msg: '用户组名为空',
            sound: false
        });
    }else{

        $("input[name='ch_box']").prop("checked",false);
        var roleId = $("#add_Group").val();
        $.ajax({
            url:"../../admin/role/roleres",
            type:"GET",
            datatype:"json",
            data:{
                "roleId":roleId
            },
            error:function(XMLHttpRequest,textStatus,errorThrown){
                console.log("载入数据出错");
            },
            success:function (data) {
                if (!data) {
                    Lobibox.notify('error', {
                        msg: '空数据',
                        sound: false
                    });
                }
                else
                {
                    Lobibox.notify('success', {
                        msg: '成功',
                        sound: false
                    });
                }
                for (var i=0; i < data.length; i++){
                    $("input[name='ch_box']").each(
                        function(){
                            if ($(this).attr("value") == data[i].resourceId){
                                $(this).prop("checked",true);
                            }
                        }
                    )
                }
            }
        });
    }
}
//modal2 comfirm
function Resmodal_sub() {
    $("#ResModal").modal("hide");
    $("#GroupModal").modal("show");
}
//modal2 exit
